using System;
using System.Collections.Generic;
using System.Text;
using Dracham.Classes;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Dracham.TileManagement.CharacterManagement
{
    public class Character
    {
        private Position position;
        private Texture2D texture;
        private Int32 speed = 500;

        private readonly Int32 renderX = (Dracham.TileWidth * 8) + Dracham.TileWidth * ((Dracham.Width / 2) - 0);
        private readonly Int32 renderY = (Dracham.TileHeight * 0) + Dracham.TileHeight * ((Dracham.Height / 2) - 0);

        private Int32 sourceX = 0;
        private Int32 sourceY = 0;
        private Boolean isMoving = false;

        private ZoneObject currentZoneObject;
        public ZoneObject CurrentZoneObject
        {
            get
            {
                return this.currentZoneObject;
            }
            set
            {
                if (value != currentZoneObject)
                {
                    currentZoneObject.OnLeave();
                    currentZoneObject = value;
                    currentZoneObject.OnEnter();
                }
            }
        }

        public Position Position
        {
            get { return this.position; }
            set { this.position = value; }
        }

        public Character(Texture2D texture)
        {
            this.texture = texture;
        }

        private Int32 lastMove;
        private Int32 animFrame;
        private movepos move = movepos.None;
        private Int32 anim = 0;
        private Int32 animx = 0;
        private enum movepos
        {
            Up,
            Left,
            Right,
            Down,
            None
        }

        public void Update(GameTime gameTime)
        {
            if (move != movepos.None)
            {
                animFrame += (Int32)gameTime.ElapsedGameTime.TotalMilliseconds;
                lastMove += (Int32)gameTime.ElapsedGameTime.TotalMilliseconds;

                if (animFrame > 100 && anim == 0)
                {
                    animFrame = 0;
                    anim = 1;
                }
                else if (animFrame > 100 && anim == 1)
                {
                    animFrame = 0;
                    anim = 0;
                }
                sourceX = animx + anim;

                if (move == movepos.Right)
                {
                    this.position.TileX = 0 - (Int32)(Dracham.TileWidth * ((float)lastMove / this.speed));
                    if (lastMove > this.speed)
                    {
                        this.position.MapX++;
                        this.position.TileX = 0;
                        move = movepos.None;
                    }
                }
                else if (move == movepos.Left)
                {
                    this.position.TileX = (Int32)(Dracham.TileWidth * ((float)lastMove / this.speed));

                    if (lastMove > this.speed)
                    {
                        this.position.MapX--;
                        this.position.TileX = 0;
                        move = movepos.None;
                    }
                }
                else if (move == movepos.Up)
                {
                    this.position.TileY = (Int32)(Dracham.TileHeight * ((float)lastMove / this.speed));

                    if (lastMove > this.speed)
                    {
                        this.position.MapY--;
                        this.position.TileY = 0;
                        move = movepos.None;
                    }
                }
                else if (move == movepos.Down)
                {
                    this.position.TileY = 0 - (Int32)(Dracham.TileHeight * ((float)lastMove / this.speed));

                    if (lastMove > this.speed)
                    {
                        this.position.MapY++;
                        this.position.TileY = 0;
                        move = movepos.None;
                    }
                }

                if (move == movepos.None)
                    lastMove = 0;
                return;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                //isMoving = true;
                //this.position.MapX++;
                animx = 2;

                if (!position.Zone.IsCollision(position.MapX + 1, position.MapY))
                    move = movepos.Right;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                //isMoving = true;
                //this.position.MapX--;
                animx = 6;

                if (!position.Zone.IsCollision(position.MapX - 1, position.MapY))
                    move = movepos.Left;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                //isMoving = true;
                //this.position.MapY--;
                animx = 0;

                if (!position.Zone.IsCollision(position.MapX, position.MapY - 1))
                    move = movepos.Up;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                //isMoving = true;
                //this.position.MapY++;
                animx = 4;

                if (!position.Zone.IsCollision(position.MapX, position.MapY + 1))
                    move = movepos.Down;
            }
        }

        public void Render(GameTime time, SpriteBatch sb)
        {
            sb.Draw(
                this.texture,
                new Microsoft.Xna.Framework.Rectangle(renderX, renderY, Dracham.TileWidth, Dracham.TileHeight),
                new Microsoft.Xna.Framework.Rectangle((sourceX * Dracham.TileWidth), (sourceY * Dracham.TileHeight), Dracham.TileWidth, Dracham.TileHeight),
                Color.White
            );
        }
    }
}
